package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.pickaddress.ChangeAddressDialog;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PostNewProductActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tv_post_new_city)
    TextView tvPostNewCity;
    @Bind(R.id.ll_post_new_city)
    LinearLayout llPostNewCity;
    @Bind(R.id.et_post_new_company_name)
    EditText etPostNewCompanyName;
    @Bind(R.id.et_post_new_product_name)
    EditText etPostNewProductName;
    @Bind(R.id.et_post_new_content)
    EditText etPostNewContent;
    @Bind(R.id.et_post_new_art)
    EditText etPostNewArt;
    @Bind(R.id.et_post_new_format)
    EditText etPostNewFormat;
    @Bind(R.id.et_post_new_price)
    EditText etPostNewPrice;
    @Bind(R.id.et_post_new_limit)
    EditText etPostNewLimit;
    @Bind(R.id.et_post_new_feature)
    EditText etPostNewFeature;
    @Bind(R.id.ll_post_new_choose_photo)
    LinearLayout llPostNewChoosePhoto;
    @Bind(R.id.et_post_new_contact)
    EditText etPostNewContact;
    @Bind(R.id.et_post_new_tel)
    EditText etPostNewTel;
    @Bind(R.id.tv_post_new_commit)
    TextView tvPostNewCommit;
    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;
    private String province="";
    private String city="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_new_product);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_post_new_product, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    private void initEvent() {
        llPostNewCity.setOnClickListener(this);
        tvPostNewCommit.setOnClickListener(this);
        llPostNewChoosePhoto.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ll_post_new_choose_photo:
                Intent intent=new Intent(this,ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.ll_post_new_city:
                ChangeAddressDialog mChangeAddressDialog = new ChangeAddressDialog(
                        this);
                mChangeAddressDialog.setAddress("浙江", "杭州");
                mChangeAddressDialog.show();
                mChangeAddressDialog.setAddresskListener(new ChangeAddressDialog.OnAddressCListener()
                                                         {
                                                             @Override
                                                             public void onClick(String province, String city) {
                                                                 PostNewProductActivity.this.province=province;
                                                                 PostNewProductActivity.this.city=city;
                                                                 tvPostNewCity.setText(province + " " + city);
                                                             }
                                                         }
                );
                break;
            case R.id.tv_post_new_commit:
                if (files == null || files.length == 0) {
                    T.showShort(this, "请选择照片");
                } else {
                    String company = etPostNewCompanyName.getText().toString();
                    String city=tvPostNewCity.getText().toString();
                    String product = etPostNewProductName.getText().toString();
                    String content = etPostNewContact.getText().toString();
                    String art = etPostNewArt.getText().toString();
                    String format = etPostNewFormat.getText().toString();
                    String price = etPostNewPrice.getText().toString();
                    String limit = etPostNewLimit.getText().toString();
                    String feature = etPostNewFeature.getText().toString();
                    String contacts = etPostNewContact.getText().toString();
                    String tel = etPostNewTel.getText().toString();
                    if (Utils.isEmpty(company)) {
                        T.showShort(this, "请输入供应商名称");
                    } else if (Utils.isEmpty(city)) {
                        T.showShort(this, "请选择地区");
                    } else if (Utils.isEmpty(product)) {
                        T.showShort(this, "请输入产品名称");
                    } else if (Utils.isEmpty(content)) {
                        T.showShort(this, "请输入主要成分");
                    } else if (Utils.isEmpty(art)) {
                        T.showShort(this, "请输入产品工艺");
                    } else if (Utils.isEmpty(format)) {
                        T.showShort(this, "请输入规格");
                    } else if (Utils.isEmpty(price)) {
                        T.showShort(this, "请输入价格");
                    } else if (Utils.isEmpty(limit)) {
                        T.showShort(this, "请输入最低起定量");
                    } else if (Utils.isEmpty(feature)) {
                        T.showShort(this, "请输入产品特点 ");
                    }else if (Utils.isEmpty(contacts)) {
                        T.showShort(this, "请输入联系人姓名");
                    } else if (Utils.isEmpty(tel)) {
                        T.showShort(this, "请输入联系人电话");
                    } else {
                        Loading.show(this, getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }

    private void commit() {
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_NEW_PRODUCT, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(PostNewProductActivity.this,e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(PostNewProductActivity.this, response);
                }
            },files,fileKeys,getParams(etPostNewCompanyName.getText().toString(),province,city,etPostNewProductName.getText().toString(),
                    etPostNewContent.getText().toString(),etPostNewArt.getText().toString(),etPostNewFormat.getText().toString(),
                    etPostNewPrice.getText().toString(),etPostNewLimit.getText().toString(),etPostNewFeature.getText().toString(),
                    etPostNewContact.getText().toString(),etPostNewTel.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public OkHttpClientManager.Param[] getParams(String title, String province, String city, String product, String content, String art,
                                                 String format, String price, String limit, String feature, String  contacts,
                                                 String tel){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(this,"id")),
                new OkHttpClientManager.Param("title", title),
                new OkHttpClientManager.Param("province", province),
                new OkHttpClientManager.Param("city", city),
                new OkHttpClientManager.Param("product", product),
                new OkHttpClientManager.Param("content", content),
                new OkHttpClientManager.Param("art", art),
                new OkHttpClientManager.Param("format", format),
                new OkHttpClientManager.Param("price", price),
                new OkHttpClientManager.Param("limit", limit),
                new OkHttpClientManager.Param("feature", feature),
                new OkHttpClientManager.Param("contacts", contacts),
                new OkHttpClientManager.Param("tel", tel)};
    }
}
